package com.ikdong.toodle.util;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import com.ikdong.toodle.dao.ContextDAO;
import com.ikdong.toodle.exception.TDException;
import com.ikdong.toodle.model.TaskContext;

public class ContextManager {
	private ContextDAO ctxDao;
	private String userId;
	private String key;
	private TaskManager tm;
	
	public ContextManager(Context context){
		ctxDao = new ContextDAO(context);
		tm = new TaskManager(context);
		userId = new AccountManager(context).getAccount().getUserid();
		key = new AuthManager(context).getKey();
	}
	
	private void syncNew() throws Exception{
		List<TaskContext> ctxs = ctxDao.get(userId,Constant.STATUS_SYNC_ADD);
		for(TaskContext lctx : ctxs){
			try {
				TaskContext sctx = ToodleHttpClient.addContext(key,lctx);
				if(sctx!=null){
					ctxDao.replace(lctx.getId().toString(), sctx);
					tm.updateContextChange(lctx.getId(), sctx.getId());
				}
			} catch (TDException e) {
				throw e;
			}
		}
	}
	
	private void syncDelete() throws Exception{
		List<TaskContext> ctxs = ctxDao.get(userId,Constant.STATUS_SYNC_DELETE);
		for(TaskContext lctx : ctxs){
			try {
				ToodleHttpClient.deleteContext(key,lctx.getId());
				ctxDao.delete(lctx.getId());
			} catch (TDException e) {
				Log.e(Constant.TAG, "syncContextDelete()", e);
				ctxDao.delete(lctx.getId());
			}
		}
	}
	
	private void syncEdit() throws Exception{
		List<TaskContext> ctxs = getOnline();
		List<Long> ids = new ArrayList<Long>();
		for(TaskContext sctx : ctxs){
			Long id = sctx.getId();
			ids.add(id);
			TaskContext lctx = ctxDao.get(id);
			if(lctx==null){
				ctxDao.add(sctx.getId(), sctx.getName(), sctx.getPrivated(), Constant.STATUS_SYNC_DONE, userId);
			}else if(lctx.getStatus()==Constant.STATUS_SYNC_MODIFY){
				TaskContext ctx = ToodleHttpClient.editContext(key, lctx);
				ctx.setStatus(Constant.STATUS_SYNC_DONE);
				ctxDao.update(ctx);
			}else if(!lctx.toString().equals(sctx.toString())){
				ctxDao.update(sctx);
			}
		}
		
		ctxDao.deleteWithoutIds(ids);
	}
	
	public void sync(boolean isSyncUpdate)throws Exception{
		syncNew();
		syncDelete();
		if(isSyncUpdate) 
			syncEdit();
	}
	
	public List<TaskContext> getOnline(){
		List<TaskContext> ctxs = new ArrayList<TaskContext>();
		ctxs.addAll(ToodleHttpClient.getContext(key));
		return ctxs;
	}
	
	public TaskContext getContext(long id){
		return ctxDao.get(id);
	}
	
	public TaskContext getContext(String name){
		return ctxDao.get(name);
	}
	
	public List<TaskContext> getContext(){
		return ctxDao.get(userId,Constant.STATUS_SYNC_ADD,Constant.STATUS_SYNC_DONE,Constant.STATUS_SYNC_MODIFY);
	}
	
	public String[] getContextNames(){
		List<TaskContext> ctxs = getContext();
		List<String> names = new ArrayList<String>();
		names.add("All");
		for(TaskContext ctx : ctxs){
			names.add(ctx.getName());
		}
		names.add("No Context");
		return names.toArray(new String[names.size()]);
	}
}
